/*
 *  Copyright (c) 2009 Baturu.COM
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.baturu.oss.dao.jpa;

import com.baturu.oss.dao.OpenPostDAO;
import com.baturu.oss.entities.comments.OpenComment;
import com.baturu.oss.entities.posts.OpenPost;
import org.sitevelocity.exceptions.NoSuchEntityException;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/**
 *
 * @author <a href="mailto:javafuns@baturu.com">javafuns</a>
 */
public class OpenPostDAOImpl implements OpenPostDAO {

    private PersistenceManager persistenceManager;

    public OpenPostDAOImpl(PersistenceManager persistenceManager) {
        this.persistenceManager = persistenceManager;
    }

    public PersistenceManager getPersistenceManager() {
        return this.persistenceManager;
    }

    @Override
    public OpenPost newPost(OpenPost openPost) {
        return this.getPersistenceManager().makePersistent(openPost);
    }

    @Override
    public void editPost(OpenPost openPost) throws NoSuchEntityException {
        OpenPost op = this.getPost(openPost.getId());
        if (op == null) {
            throw new NoSuchEntityException("Post with ID [" + openPost.getId() + "] not exists.");
        }

        this.getPersistenceManager().makePersistent(openPost);
    }

    @Override
    public void removePost(String id) throws NoSuchEntityException {
        this.getPersistenceManager().deletePersistent(this.getPost(id));
    }

    @Override
    public List<OpenPost> findPosts(int maxResults, int firstResult, String orderByDesc) {
        String querySQL = "select from " + OpenPost.class.getName();
        Query query = this.getPersistenceManager().newQuery(querySQL);
        if (maxResults >= 0) {
            if (firstResult < 0) {
                firstResult = 0;
            }
            query.setRange(firstResult, firstResult + maxResults);
        }
        if (orderByDesc != null && !"".equals(orderByDesc)) {
            orderByDesc = "postCreateDate desc";
        }
        query.setOrdering(orderByDesc);
        List<OpenPost> posts = (List<OpenPost>)query.execute();
        return (posts == null ? new ArrayList<OpenPost>(0) : posts);
    }

    @Override
    public OpenPost getPost(String id) {
        return this.getPersistenceManager().getObjectById(OpenPost.class, id);
    }

    @Override
    public int getPostCount() {
        String querySQL = "select count(id) from " + OpenPost.class.getName();
        Query query = this.getPersistenceManager().newQuery(querySQL);
        Number count = (Number) query.execute();
        return count.intValue();
    }

    @Override
    public OpenComment newComment(OpenComment oc) {
        return this.getPersistenceManager().makePersistent(oc);
    }

    @Override
    public OpenComment getComment(String id) {
        return this.getPersistenceManager().getObjectById(OpenComment.class, id);
    }

    @Override
    public void removeComment(String id) throws NoSuchEntityException {
        this.getPersistenceManager().deletePersistent(this.getComment(id));
    }
}
